//
//  ChangeBingInfoController.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/9.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "ChangeBingInfoController.h"
#import "VerifyPswViewController.h"
#import "VerifyCodeBtn.h"
#import "BaseTextField.h"
#import "CYXBaseRequest.h"
#import "PicVerCodeView.h"
#import "CheckInput.h"
#import "CountryCodeSelectViewController.h"

@interface ChangeBingInfoController ()
@property (weak, nonatomic) IBOutlet UIButton *phonePreStr;
@property (weak, nonatomic) IBOutlet UIImageView *downArr;
@property (weak, nonatomic) IBOutlet UITextField *infoInput;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *infoInputLeft;
@property (weak, nonatomic) IBOutlet VerifyCodeBtn *pvcBtn;
@property (weak, nonatomic) IBOutlet BaseTextField *phoneInput;
@property (weak, nonatomic) IBOutlet BaseTextField *pvcInput;
@property (weak, nonatomic) IBOutlet UIButton *codeSelectBtn;

@property (nonatomic, copy) NSString *countryCode;

@end

@implementation ChangeBingInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.countryCode = @"86";
    [self configPage];
}

- (void)configPage{
    if (self.bindInfoType == 0) {
        self.title = LCLocalString(@"change_sjh");
        self.phonePreStr.hidden = NO;
        self.downArr.hidden = NO;
        self.infoInput.placeholder = LCLocalString(@"phone_num");
        self.infoInputLeft.constant = 77;
        
    }else if (self.bindInfoType == 1){
        self.title = @"更换邮箱";
        self.phonePreStr.hidden =  YES;
        self.downArr.hidden = YES;
        self.infoInput.placeholder = @"邮箱";
        self.infoInputLeft.constant =  15;
        
    }
}


- (void)setupUI{
    [super setupUI];
    LRWeakSelf(self);
    self.pvcBtn.clickBlock = ^{
        [weakself getGvc];
//        [weakself getPvc_net];
        [weakself.view endEditing:YES];
//        [weakself checkIsRegistered];
        
    };
    [self.pvcBtn configVerifyBtn:60];
    
    self.phoneInput.placeholder = LCLocalString(@"plh_qsrsjh");
    self.phoneInput.placeholderColor = PlhColor;
    self.pvcInput.placeholder = LCLocalString(@"plh_qsryzm");
    self.pvcInput.placeholderColor = PlhColor;
}
- (IBAction)doneAction:(id)sender {
    if(![self checkInput]) return;
    
    VerifyPswViewController *vc = LoadVCFormXib(VerifyPswViewController);
    [self.navigationController pushViewController:vc animated:YES];
}




- (BOOL)checkInput{
    if (![CheckInput isPhoneNum:self.phoneInput.text]) {
//        [self showErrorMsg:@"请输入正确的手机号"];
        LCInfoHUD(LCLocalString(@"ts_sjhyw"),1);
        return NO;
    }
    if (!self.pvcInput.text.length) {
//        [self showErrorMsg:@"请输入正确的手机号"];
        LCInfoHUD(self.pvcInput.placeholder,1);
        return NO;
    }

    return YES;
}


- (void)getGvc{
    if (![self checkPhone]) return;
    PicVerCodeView *view = [[PicVerCodeView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT)];
    view.gvcApi = API_getGvc;
    LRWeakSelf(self);
    view.actionBlock = ^(NSInteger index,NSString *gvCode) {
        CYXLog(@"%zd",index);
        if(index == 1){
            [weakself.view endEditing:YES];
            [weakself getPvc_net:gvCode];
        }
    };
    
    [view showInView:self.navigationController.view];
}

- (BOOL)checkPhone{
    if (![CheckInput isPhoneNum:self.phoneInput.text]) {
//        [self showErrorMsg:@"请输入正确的手机号"];
        LCInfoHUD(LCLocalString(@"ts_sjhyw"),1);
        return NO;
    }
    return YES;
}

- (void)getPvc_net:(NSString *)gvCode{
    if (![self checkPhone]) return;
    
    NSDictionary *param = @{@"phone":self.phoneInput.text,@"gvc":gvCode};
    
        [CYXBaseRequest postWithUrl:API_sendRegisterPvc param:param resultClass:nil success:^(id res) {
            if ([res isEqual: RequestErrorCode]) {
                
            }else{
                LCInfoHUD(@"驗證碼已發送", 1);
                [self.pvcBtn beginRun];
            }
            CYXLog(@"%@",res)
        } failure:^(NSError *error) {
            CYXLog(@"%@",error)
        }];
}

- (IBAction)countrySelectAction:(id)sender {
    CountryCodeSelectViewController *vc = LoadVCFormXib(CountryCodeSelectViewController);
    vc.backBlock = ^(NSDictionary * _Nonnull countryInfo) {
        self.countryCode = countryInfo[@"tel"];
        [self.codeSelectBtn setTitle:[NSString stringWithFormat:@"+%@",self.countryCode] forState:(UIControlStateNormal)];
    };
    //设置模态视图弹出样式
    vc.modalPresentationStyle = UIModalPresentationOverFullScreen;

    [self presentViewController:vc animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
